package com.lixn.homework.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginIntercepter implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求的RUi:去除http:localhost:8080这部分剩下的
        String uri = request.getRequestURI();
        //除了/login是可以公开访问的，其他的URL都进行拦截控制
        if (uri.indexOf("/login") >= 0 || uri.indexOf("/tologin") >= 0) {
            return true;
        }

        HttpSession session = request.getSession();
        if(session.getAttribute("admin") == null){
            //不符合条件的给出提示信息，并转发到登录页面
            //request.setAttribute("msg", "您还没有登录，请先登录！");
            response.sendRedirect(request.getContextPath()+"/resume/login");
            return false;
            //request.getRequestDispatcher("/WEB-INF/login.html").forward(request, response);
        }
        return true;
    }

}
